'use strict';
const fs = require('fs');
const path = require('path');
const awaitWriteStream = require('await-stream-ready').write;
const sendToWormhole = require('stream-wormhole');
const Controller = require('egg').Controller;

class Admincontroller extends Controller {
  async index() {

    let member=this.ctx.session.member;
    console.log(member);
    if(!member){
      return this.ctx.response.redirect('/login');
    }
   
    const dataList = {
      list: [
        { id: 1, title: 'this is news 1', url: '/news/1' },
        { id: 2, title: 'this is news 2', url: '/news/2' }
      ]
    };
     dataList.id=member.id;
     dataList.name=member.name;
     dataList.password=member.password;
    const content1s = await this.ctx.service.admin.showcontent1();
    dataList.content1s = content1s;
    const content2s = await this.ctx.service.admin.showcontent2();
    dataList.content2s = content2s;
    const content3s = await this.ctx.service.admin.showcontent3();
    dataList.content3s = content3s;
    const content4s = await this.ctx.service.admin.showcontent4();
    dataList.content4s = content4s;
    const content5s = await this.ctx.service.admin.showcontent5();
    dataList.content5s = content5s;

    const image1s = await this.ctx.service.admin.showImage1();
    dataList.image1s = image1s;
    const image2s = await this.ctx.service.admin.showImage2();
    dataList.image2s = image2s;
    const image3s = await this.ctx.service.admin.showImage3();
    dataList.image3s = image3s;
    const image4s = await this.ctx.service.admin.showImage4();
    dataList.image4s = image4s;
    const image5s = await this.ctx.service.admin.showImage5();
    dataList.image5s = image5s;
    const image6s = await this.ctx.service.admin.showImage6();
    dataList.image6s = image6s;
    const image7s = await this.ctx.service.admin.showImage7();
    dataList.image7s = image7s;
    const image8s = await this.ctx.service.admin.showImage8();
    dataList.image8s = image8s;
    //console.log(image1s);
    await this.ctx.render('admin.html', { dataList });
  }
  async upcontent1() {
    const postdata = this.ctx.request.body;
    const data = await this.ctx.service.admin.createContent1(postdata);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  async showcontent1() {
    const dataList = {
      list: [
        { id: 1, title: 'this is news 1', url: '/news/1' },
        { id: 2, title: 'this is news 2', url: '/news/2' }
      ]
    };
    const content1s = await this.ctx.service.admin.showcontent1();
    await this.ctx.render('admin.html', content1s);
  }
  async delcontent1() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delcontent1(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //content2
  async upcontent2() {
    const postdata = this.ctx.request.body;
    const data = await this.ctx.service.admin.createContent2(postdata);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delcontent2() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delcontent2(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  //content3
  async upcontent3() {
    const postdata = this.ctx.request.body;
    const data = await this.ctx.service.admin.createContent3(postdata);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delcontent3() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delcontent3(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //content4
  async upcontent4() {
    const postdata = this.ctx.request.body;
    const data = await this.ctx.service.admin.createContent4(postdata);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delcontent4() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delcontent4(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //content5
  async upcontent5() {
    const postdata = this.ctx.request.body;
    const data = await this.ctx.service.admin.createContent5(postdata);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delcontent5() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delcontent5(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  //image1
  async upimage1() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image1', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage1({ url: filename ,link:encodeURIComponent(stream.fields.link)});
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage1() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage1(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image2
  async upimage2() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image2', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage2({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage2() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage2(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image3
  async upimage3() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image3', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage3({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage3() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage3(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image4
  async upimage4() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image4', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage4({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage4() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage4(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image5
  async upimage5() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image5', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage5({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage5() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage5(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image6
  async upimage6() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image6', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage6({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage6() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage6(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image7
  async upimage7() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image7', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage7({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage7() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage7(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }
  //image8
  async upimage8() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const filename = name + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/image8', filename);
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }
    const data = await this.ctx.service.admin.createImage8({ url: filename });
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  async delimage8() {
    let id = this.ctx.query.id;

    const data = await this.ctx.service.admin.delImage8(id);
    if (data) {
      return await this.ctx.render('result.html');
    }
    await this.ctx.render('error.html');
  }

  //
  //logo
  async uplogo() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img/logos', 'logo.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
  //footer1
  async upfooter1() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img', 'footer1.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
  //footer2
  async upfooter2() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img', 'footer2.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
  //footer3
  async upfooter3() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img', 'footer3.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
/////////////////////
  //upmodel1
  async upmodel1() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img/products', 'model1.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
    //upmodel2
    async upmodel2() {
      const stream = await this.ctx.getFileStream();
      const name = new Date().getTime();
      //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
      const target = path.join(this.config.baseDir, 'app/public/img/products', 'model2.png');
      const writeStream = fs.createWriteStream(target);
      try {
        await awaitWriteStream(stream.pipe(writeStream));
      } catch (err) {
        await sendToWormhole(stream);
        throw err;
      }
  
      await this.ctx.render('result.html');
    }
      //model3
  async upmodel3() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img/products', 'model3.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
    //model4
    async upmodel4() {
      const stream = await this.ctx.getFileStream();
      const name = new Date().getTime();
      //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
      const target = path.join(this.config.baseDir, 'app/public/img/products', 'model4.png');
      const writeStream = fs.createWriteStream(target);
      try {
        await awaitWriteStream(stream.pipe(writeStream));
      } catch (err) {
        await sendToWormhole(stream);
        throw err;
      }
  
      await this.ctx.render('result.html');
    }
      //model5
  async upmodel5() {
    const stream = await this.ctx.getFileStream();
    const name = new Date().getTime();
    //const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
    const target = path.join(this.config.baseDir, 'app/public/img/products', 'model5.png');
    const writeStream = fs.createWriteStream(target);
    try {
      await awaitWriteStream(stream.pipe(writeStream));
    } catch (err) {
      await sendToWormhole(stream);
      throw err;
    }

    await this.ctx.render('result.html');
  }
}

module.exports = Admincontroller;